﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructureFamilyUiUtils.h"
#include "IFamily.h"
#include "FamilyUtils.h"
#include "FamilyLoadOptionsWithUI.h"
#include "IUserTransaction.h"
#include "UiCommonDialog.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

bool StructureFamilyUiUtils::CreateFamilies(IDocument* pDoc, const FamilyCreators& familyCreators)
{
    FOR_EACH(familyCreator, familyCreators)
    {
        std::wstring familyName = familyCreator.first;
        const IFamily* pFamily = IFamily::GetFamilyByName(pDoc, familyName);
        if (pFamily)
            continue;

        std::wstring familyFilePath = familyCreator.second(familyName);
        DBG_WARN_AND_CONTINUE_UNLESS(!familyFilePath.empty(), L"familyFilePath为空",L"GDMPLab",L"2024-03-30");

        pFamily = StructureFamilyUiUtils::LoadFamily(pDoc, familyFilePath);
        DBG_WARN_AND_CONTINUE_UNLESS(pFamily, L"pFamily为空",L"GDMPLab",L"2024-03-30");
    }

    return true;
}

const IFamily* StructureFamilyUiUtils::LoadFamily(IDocument* pDoc, const std::wstring& familyFilePath)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(!familyFilePath.empty(), L"familyFilePath为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> opTransaction = IUserTransaction::Create(pDoc, GBMP_TR(L"加载族文件"), true);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opTransaction, L"opTransaction为空",L"GDMPLab",L"2024-03-30");

    std::wstring errorMsg;
    FamilyLoadOptionsWithUI familyLoadOptions(gcmp::FamilyFoundStrategy::FF_Overwrite);
    LoadFamilyInfos loadFamilyInfos(familyFilePath, 0, 0, [](std::map<ElementId, bool>&) { return true; });
    LoadFamilyResult loadFamilyResult;
    const IFamily* pFamily = FamilyUtils::LoadFamily(pDoc, loadFamilyInfos, errorMsg, &familyLoadOptions, &loadFamilyResult);
    if (!pFamily)
    {
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pFamily, L"加载族文件失败",L"GDMPLab",L"2024-03-30");
        opTransaction->Rollback();
        return nullptr;
    }
    opTransaction->Commit();

    return pFamily;
}

